package com.exteam.common.view.dialog;

import android.app.ProgressDialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.exteam.common.R;
import com.exteam.common.util.CommLogger;


/**
 * 载入的Dialog
 *
 * @author sean.yanghy
 * @version v1.0
 * @created 2014-07-06 13:56:23
 */
public class LoadingDialog {

    private Context ctx;
    ProgressDialog pd;
    View dialogView;
    TextView loadingMessage;
    ProgressBar progressBarLarge;
    ProgressBar progressBarSeek;
    Button btnClose;
    CloseListener closeListener;

    public static final int MSG_SHOW = 100;
    public static final int MSG_CLOSE = 300;

    public static interface CloseListener {
        void onCloseDialog();
    }

    public LoadingDialog(Context ctx) {
        this.ctx = ctx;

        LayoutInflater inflater = (LayoutInflater) ctx.getSystemService(ctx.LAYOUT_INFLATER_SERVICE);
        dialogView = inflater.inflate(R.layout.view_loading, null);
        loadingMessage = (TextView) dialogView.findViewById(R.id.loadingMessage);
        progressBarLarge = (ProgressBar) dialogView.findViewById(R.id.progressBarLarge);
        progressBarSeek = (ProgressBar) dialogView.findViewById(R.id.progressBarSeek);
        btnClose = (Button) dialogView.findViewById(R.id.btnClose);
        btnClose.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                if (closeListener != null) {
                    closeListener.onCloseDialog();
                }
            }
        });
        btnClose.setVisibility(View.GONE);
        progressBarSeek.setVisibility(View.GONE);
        pd = new ProgressDialog(ctx, R.style.CustomProgressDialog);
        pd.setCanceledOnTouchOutside(false);
    }

    public ProgressDialog getDialog() {
        return pd;
    }

    public void setOnCloseListener(CloseListener closeListener) {
        this.closeListener = closeListener;
    }

    public void setCloseButtonShow(int tag) {
        btnClose.setVisibility(tag);
    }

    public void openShowProgressMessage(int resString) {
        if (pd != null) {
            progressBarLarge.setVisibility(View.GONE);
            progressBarSeek.setVisibility(View.VISIBLE);
            btnClose.setVisibility(View.VISIBLE);
            if (pd.isShowing()) {
                loadingMessage.setText(resString);
            } else {
                pd.show();
                pd.setContentView(dialogView);
                loadingMessage.setText(resString);
            }
        }
    }

    public void onProgressShow(int progress) {
        if (pd != null && progressBarSeek != null) {
            progressBarSeek.setProgress(progress);
        }
    }

    public void closeShowProess() {
        if (pd != null) {
            progressBarSeek.setVisibility(View.GONE);
            btnClose.setVisibility(View.GONE);
            progressBarLarge.setVisibility(View.VISIBLE);
        }
    }

    public void show(int resString) {
        CommLogger.d("show.... ");
        if (pd != null) {
            if (pd.isShowing()) {
                loadingMessage.setText(resString);
                loadingMessage.setVisibility(View.VISIBLE);
                loadingMessage.invalidate();
            } else {
                pd.show();
                pd.setContentView(dialogView);
                loadingMessage.setText(resString);
                loadingMessage.setVisibility(View.VISIBLE);
                loadingMessage.invalidate();
            }
        }
    }

    public void show(String resString) {
        CommLogger.d("show.... ");
        if (pd != null) {
            if (pd.isShowing()) {
                loadingMessage.setText(resString);
                loadingMessage.setVisibility(View.VISIBLE);
                loadingMessage.invalidate();
            } else {
                try {
                    pd.show();
                    pd.setContentView(dialogView);
                    loadingMessage.setText(resString);
                    loadingMessage.setVisibility(View.VISIBLE);
                    loadingMessage.invalidate();
                } catch (Exception e) {

                }
            }
        }
    }

    public void dissmis() {
        try {
            if (pd != null && pd.isShowing()) {
                pd.dismiss();
            }
        } catch (Exception e) {

        }
    }

}
